
#ifndef CALCULATOR_HPP
#define CALCULATOR_HPP

#include <iostream>
#include <string>
#include "Protocal.hpp"

class Calculator
{
public:
    Calculator()
    {
    }

    response Execute(const request &req)
    {
        response res;
        switch (req.Oper())
        {
        case '+':
            res.Setresult(req.X() + req.Y());
            res.Setcode(0);
            break;
        case '-':
            res.Setresult(req.X() - req.Y());
            res.Setcode(0);
            break;
        case '*':
            res.Setresult(req.X() * req.Y());
            res.Setcode(0);
            break;
        case '/':
        {
            if (req.Y() == 0)
            {
                res.Setcode(1); // 1 就是除0
            }
            else
            {
                res.Setresult(req.X() / req.Y());
            }
        }
        break;
        case '%':
        {
            if (req.Y() == 0)
            {
                res.Setcode(2); // 2代表%0
            }
            else
            {
                res.Setresult(req.X() + req.Y());
                res.Setcode(0);
            }
        }
        break;
        default:
            res.Setcode(3); // 3表示无法识别
            break;
        }
        return res;
    }
    Json::Value Execute(const Json::Value &req)
    {
        std::cout << "进来了Json::Value Execute(const Json::Value &req)" << std::endl;
        std::string req1;
        zj::JsonAPI::serialize(req, &req1);
        std::cout << req1 << std::endl;
        std::cout << req["OPER"].asInt() << std::endl;
        Json::Value res;
        switch (req["OPER"].asInt())
        {
        case '+':
            std::cout << "case +:" << std::endl;
            res["RESULT"] = req["X"].asInt() + req["Y"].asInt();

            res["RCODE"] = 0;
            break;
        case '-':
            res["RESULT"] = req["X"].asInt() - req["Y"].asInt();

            res["RCODE"] = 0;
            break;
        case '*':
            res["RESULT"] = req["X"].asInt() * req["Y"].asInt();

            res["RCODE"] = 0;
            break;
        case '/':
        {
            if (req["Y"].asInt() == 0)
            {
                res["RCODE"] = 1; // 1 就是除0
            }
            else
            {
                res["RESULT"] = req["X"].asInt() / req["Y"].asInt();
            }
        }
        break;
        case '%':
        {
            if (req["Y"].asInt() == 0)
            {
                res["RCODE"] = 2; // 2代表%0
            }
            else
            {
                res["RESULT"] = req["X"].asInt() % req["Y"].asInt();
                res["RCODE"] = 0;
            }
        }
        break;
        default:
            res["RCODE"] = 3; // 3表示无法识别
            break;
        }
        return res;
    }
    ~Calculator()
    {
    }
};

#endif
